package com.nc.shop.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nc.shop.service.dao.LandDao;
import com.nc.shop.service.dao.LandLeanDao;
import com.nc.shop.service.model.entity.ClassifyEntity;
import com.nc.shop.service.model.entity.LandEntity;
import com.nc.shop.service.model.entity.LandLeanEntity;
import com.nc.shop.service.service.LandService;
import com.plug.result.ReqPage;
import com.plug.result.RetResponse;
import com.plug.result.RetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@Service
public class LandServiceImpl implements LandService {
    @Autowired
    LandDao landDao;
    @Autowired
    LandLeanDao landLeanDao;

    @Override
    public RetResult findPage(ReqPage reqPage) {
        JSONObject req = Optional.ofNullable(reqPage.getCommon()).orElseGet(JSONObject::new);
        LandEntity entity = JSON.parseObject(req.toJSONString(), LandEntity.class);
        int page = reqPage.getPage() - 1;
        int size = reqPage.getLimit();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "modifiedTime");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<LandEntity> example = Example.of(entity, matcher);
        Page<LandEntity> pages = landDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", pages.getContent(), (int) pages.getTotalElements());
    }

    @Override
    public RetResult findLeanPage(ReqPage reqPage) {
        JSONObject req = Optional.ofNullable(reqPage.getCommon()).orElseGet(JSONObject::new);
        LandLeanEntity entity = JSON.parseObject(req.toJSONString(), LandLeanEntity.class);
        int page = reqPage.getPage() - 1;
        int size = reqPage.getLimit();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "modifiedTime");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<LandLeanEntity> example = Example.of(entity, matcher);
        Page<LandLeanEntity> pages = landLeanDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", pages.getContent(), (int) pages.getTotalElements());
    }

    @Override
    public Boolean findLandStatus(String landId) {
        Optional<LandEntity> opt = landDao.findById(landId);
        if (opt.isPresent()) {
            if(opt.get().getStatus()==1){
                return true;
            }
        }
        return false;
    }
}
